<?php
/**
 * Create By: PhpStorm
 * User: niechenguang
 * File: GetCircle.php
 * Date: 2017/1/2
 * Time: 23:13
 */

namespace app\api\model\page\note;

use app\api\model\dao\Circle;
use app\api\model\dao\Note;
use app\api\model\dao\NotePraise;
use app\api\model\dao\NoteReply;
use app\api\model\data\note\NoteData;
use app\library\Flag;
use app\library\PageBase;
use think\Error;
use think\Exception;
use think\Validate;

class GetApproveNoteList extends PageBase
{

    private $objNote;
    private $objNoteReply;
    private $objNoteData;
    private $objNotePraise;
    private $objCircle;

    public function __construct()
    {
        parent::__construct();

        $this->objNote = new Note();
        $this->objNoteReply = new NoteReply();
        $this->objNoteData = new NoteData();
        $this->objNotePraise = new NotePraise();
        $this->objCircle = new Circle();
    }

    /**
     * @param $param
     * @return array|bool
     * @DESC 验证接口字段
     */
    public function checkParam($param)
    {
        $rule = [
            'cir_id' => 'require',
        ];
        $msg = [
            'cir_id.require' => '请选择圈子',
        ];

        $validate = new Validate($rule, $msg);
        $result = $validate->check($param);
        if (!$result) {
            return $validate->getError();
        }
        return '';
    }

    /**
     * page 入口
     *
     * @param $param
     * @return array
     * @throws Exception
     */
    public function excute($param)
    {
        $ret = $this->checkToken($param);
        if (false === $ret) {
            return $this->getRes(\app\library\Error::ERR_NO_AUTH);
        }

        $this->data['list'] = [];

        // 获取我（版主）的圈子
        $conds = [
            'creator' => ['=', $this->user_id],
            'approve_status' => ['=', Flag::CONST_CIRCLE_APPROVE_PASS],
            'is_delete' => ['=', Flag::CONST_CIRCLE_DELETE_NOT],
        ];
        $ret = $this->objCircle->getCircleList($conds, ['id']);
        if (empty($ret)) {
            return $this->getRes();
        }

        $arrCirid = [];
        foreach ($ret as $val) {
            $arrCirid[] = $val['id'];
        }

        $note = $this->objNoteData->approveNote($arrCirid, $this->offset, $this->limit);
        if (!empty($note)) {
            $this->data['list'] = $note;
        }

        return $this->getRes();
    }
}
